﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace BankSystemWithCSharp
{
    public class BankAccount
    {
        public readonly string AccountNumber;
        private readonly string passwordMD5;
        private double money = 0;
        public double Money
        {
            get { return money; }
            set { 
                money = value;
            }
        }

        public BankAccount(string accountNumber, string password)
        {
            this.AccountNumber = accountNumber;
            this.passwordMD5 = ToMD5(password);
        }
        public void ConfirmPassword(string password)
        {
            if (passwordMD5 != ToMD5(password))
                throw new DataBaseException(DataBaseExceptionType.PasswordNotMatch, "密码不匹配");
        }

        private static string ToMD5(string strs)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(strs);//将要加密的字符串转换为字节数组
            byte[] encryptdata = md5.ComputeHash(bytes);//将字符串加密后也转换为字符数组
            return Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为加密字符串
        }

        public override string ToString()
        {
            return AccountNumber.ToString();
        }
    }
}